// 商品管理脚本
$(function() {
	$("#searchid").linkbutton({
				text : '查询',
				iconCls : 'icon-search'
			});
	$('#messagegrid').datagrid({
		title : '您所在的位置：商家管理&nbsp;&nbsp;--&gt;&gt;&nbsp;&nbsp;活动管理',
		idField : 'id',
		width : 1000,
		height : 500,
		nowrap : false,
		striped : true,
		collapsible : false,
		url : '../brand/searchFavorable.shtml?bid='+bid,
		frozenColumns : [[{
					field : 'ck',
					title : '选择',
					checkbox : true
				}]],
		columns : [[{
					title : '基本信息',
					colspan : 5
				}, {
					field : 'opt',
					title : '操作',
					width : 160,
					align : 'center',
					rowspan : 2,
					formatter : function(value, rec) {
					if (rec.id == undefined)
							return null;
						else
							return '<span style="color:red"><span onclick=onmodify("'
									+ rec.id
									+ '")><font style="cursor:pointer;color:blue;">修改</font></span> <span onclick=ondelete("'
									+ rec.id + '")><font style="cursor:pointer;color:blue;">删除</font></span></span><hr>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select id="aud_'+rec.id +'"><option value="1">审核通过</option><option value="2">审核不通过</option></select>&nbsp;<span onclick=onAutding("'
									+ rec.id + '")><font style="cursor:pointer">审核</font></span></span>';
					}
				}], [{
					field : 'title',
					title : '活动名称',
					mapping : 'name',
					align : 'center',
					width : 180
				},{
					field : 'imgSrc',
					title : '活动图片',
					mapping : 'name',
					align : 'center',
					width : 165,
					formatter : function(value, rec) {
						if(value == undefined)
							return "";
						if(value == ""){
							return '暂无图片';
						}
						return '<img src="../'+value+'" width="162px" height="113px"/>';
					}
				}, 
				{
					field : 'startTimeString',
					title : '开始时间',
					width : 120,
					mapping : 'startTimeString',
					align : 'center' 
				},{
					field : 'endTimeString',
					title : '结束时间',
					align : 'center',
					width : 120 
				},{
					field : 'autding',
					title : '核实状态',
					width : 60,
					formatter : function(value, rec) {
						if(value == undefined)
							return "";
						if(value==1)return "通过";
					    if(value==2)return "未通过";
					    return  "未审核";
					},
					mapping : 'authorName',
					align : 'center' 
				}

		]],
		pagination : true,
		rownumbers : true,
		queryParams : {
			name : '',
			auditingState : '-1',
			category:'-1',
			price1:'-1',
			price2:'-1',
			discode:'-1'
		},
		toolbar : [{
					id : 'btnadd',
					text : '发布信息',
					iconCls : 'icon-add',
					handler : function() {
						$(this).attr("href", '../brand/searchFavorableById.shtml?bid='+bid);
					}
				}, '-', {
					id : 'btnsave',
					text : '删除信息',
					iconCls : 'icon-remove',
					handler : function() {
						var records = $('#messagegrid')
								.datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要删除的记录');
							return;
						}
						for (var i = 0; i < records.length; i++) {
							if (records[i].id == undefined
									|| records[i].id == "undefined") {
								alert('此条记录已不存在');
								return;
							}
							ids += records[i].id;
							ids += '_';
						}
						if (confirm("确定删除所选信息吗?")) {
						var url1 = "../brand/deleteFavorable.shtml";
						$.ajax({
									url : url1,
									data : "idString=" + ids + "&type=1",
									success : function(msg) {
										var obj = eval('(' + msg + ')');
										var result = obj[0]["message"];
										alert(result);
										$('#messagegrid').datagrid("reload");
									}
								});
						}
					}
				},'-',{
					id : 'btnback',
					text : '返回上一层',
					iconCls : 'icon-back',
					handler : function() {
						$(this).attr("href", '../shoper/branlist.jsp?menuCls=331'+'&name='+name+'&auding='+auding);
					}
				}
				]
		}
	);
	$("body").keydown(function(){
	     if(event.keyCode==13)
         {
           onSearch();
         }
	    });
});

function onSearch() {
    //var category= $("#category").combobox("getValue");
	var queryParams = $('#messagegrid').datagrid("options").queryParams;
	queryParams.name = $("#name").val();
	//queryParams.category =category;
	$('#messagegrid').datagrid("reload");
}

function ondelete(id) {
	if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	if (confirm("确定删除此条信息吗?")) {
		var url1 = "../brand/deleteFavorable.shtml";
		$.ajax({
				url : url1,
				data : "idString=" + id,
				success : function(msg) {
					var obj = eval('(' + msg + ')');
					var result = obj[0]["message"];
					alert(result);
					$('#messagegrid').datagrid("reload");
				}
			});
	}
}
function onmodify(id) {
	if (id == "undefined") {
		alert("此条记录已被删除");
		return;
	}
	window.location.href = "../brand/searchFavorableById.shtml?bid="+bid+"&id=" + id;
}
function onAutding(id){
		if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	if (confirm("确定审核此条信息吗?")) {
        var autding=document.getElementById("aud_"+id).value;
		var url1 = "../brand/audFavorable.shtml";
			$.ajax({
					url : url1,
					data : "idString=" + id+"&autding="+autding ,
					cache:false,
					success : function(msg) {
   					   var obj = eval('(' + msg + ')');
                       var result = obj[0]["message"];
                         if(!result){
                                     alert("审核失败");
                                     return;
		                     }
		                  alert("审核成功");
						$('#messagegrid').datagrid("reload");
					}
				});
	}
}